<?php


namespace app\job;

use app\admin\model\Equity;
use app\admin\model\Order;
use app\common\model\User;
use app\admin\model\UserRelation;
use think\facade\Log;
use think\queue\Job;
use app\service\FXApiService;
use app\service\IyunBinkService;

class DayEquityJob
{
    /**
     *每日定时任务分润
     */
    public function fire(Job $job, $data)
    {
        $isJobDone = $this->doTask($data);
        if ($isJobDone) {
            $job->delete();
        } else {
            $job->release(2);
        }
    }

    /**
     *
     * task具体执行逻辑
     */
    public function doTask($data)
    {
        $EquityModel = new Equity();
        $EquityFind = $EquityModel->where('id', $data['equity_id'])->find();
        //$res = Db::table('chd_auth_role')->insert($params);
        Log::info("==每日分润任务执行==" . json_encode($data));
        if ($EquityFind) {
            $orderModel = new Order();
            $userModel = new User();
            $orderFind = $orderModel->where('id', $EquityFind->order_id)->find();
            $userFind = $userModel->where('id', $EquityFind->user_id)->find();
            if ($orderFind) {
                if ($orderFind->status == 4) {
                    $EquityFind->status = 4;
                    $EquityFind->save();
                    $userFind->hlat_money -= $EquityFind->fee;
                    $userFind->save();
                  
                }
                if ($orderFind->status == 1) {
                    $time = date('Ymd', time());
                    if ($EquityFind->date == $time) {
                        $EquityFind->status = 2;
                        $EquityFind->save();
                        $userFind->hlat_money -= $EquityFind->fee;
                        $userFind->save();
                        $userModel->money($EquityFind->fee, $EquityFind->user_id, $EquityFind->order_id . '加');
                        $or = $EquityModel->where('order_id',  $EquityFind->order_id)->where('status', 1)->find();
                        if (!$or) {
                            $orderFind->status = 2;
                            $orderFind->save();
                        }
                      
                    }
                }
                Log::info("==每日分润任务结束1==" . json_encode($data));
                return true;
            }else{
                Log::info("==每日分润任务结束2==" . json_encode($data));
                return true;
            }
        }
        return false;
    }
}
